﻿using System;
using System.Windows.Forms;
using Zove.Runtime;
using EmbeddedSharp.Core;
using WeifenLuo.WinFormsUI.Docking;

namespace Zove.Desktop {
    public partial class ResolutionLogWindow : DockContent {

        private RuntimeFactor rf;

        private int count = 0;

        public ResolutionLogWindow() {
            InitializeComponent();
        }

        public ResolutionLogWindow(RuntimeFactor rf)
            : this() {
            this.rf = rf;
            this.Text = "Solution Log - " + rf.Name;
        }

        public void Show(RuntimeFactor rf) {
            if (rf.Status == RuntimeFactorStatus.Input) {
                this.gvLog.Rows.Add(new string[] { (++count).ToString(), rf.Name, "Input" });
                return;
            } else if (rf.Status == RuntimeFactorStatus.Unknown) {
                throw new Exception("");
            }

            foreach (RuntimeFactor rff in rf.SolvedRule.Equation.Factors) {
                if (rff.Name == rf.Name) {
                    continue;
                }

                this.Show(rff);
            }

            string expr = DefaultDependencyManager.Instance.ExpressionTranslator.Translate(
                rf.SolvedRule.Root);
            this.gvLog.Rows.Add(new string[] {
                (++count).ToString(), rf.Name, expr });
        }
    }
}
